package com.mimikko.example;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

public class SleepCompanionConfigActivity extends Activity {
    private CheckBox chkAdaptOLED;
    private CheckBox chkDimScreen;
    private SeekBar numSleepTimer;
    private TextView lblSleepTimer;
    private void initUI(){	 chkAdaptOLED = (CheckBox)findViewById(R.id.chkAdaptOLED);
        chkDimScreen = (CheckBox)findViewById(R.id.chkDimScreen);
        numSleepTimer = (SeekBar)findViewById(R.id.numSleepTimer);
        lblSleepTimer = (TextView)findViewById(R.id.lblSleepTimer);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sleep_config);
        initUI();
        ActivityUtils.transparentSystemUI(this);
        SpUtils sp = SpUtils.on(this);
        chkDimScreen.setChecked(sp.isDimScreen());
        chkAdaptOLED.setChecked(sp.isAdaptOLED());
        numSleepTimer.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                lblSleepTimer.setText("入眠计时器："+((progress+1) * 15)+"分钟");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        numSleepTimer.setProgress(sp.getSleepCountDown() / 15 - 1);
        lblSleepTimer.setText("入眠计时器："+sp.getSleepCountDown()+"分钟");
    }

    public void dismissDialog(View view) {
        finish();
    }

    public void start(View view) {
        SpUtils sp = SpUtils.on(this);
        sp.setAdaptOLED(chkAdaptOLED.isChecked());
        sp.setDimScreen(chkDimScreen.isChecked());
        sp.setSleepCountDown((numSleepTimer.getProgress() + 1) * 15);
        startActivity(new Intent(this,SleepCompanionActivity.class));
        finish();
    }
}

class SpUtils{
    public static SpUtils on(Context ctx){
        return new SpUtils(ctx);
    }
    private SpUtils(Context ctx){
        this.sp = ctx.getSharedPreferences("114514",0);
    }
    private SharedPreferences sp;

    public boolean isAdaptOLED() {
        return sp.getBoolean("adaptOLED",false);
    }

    public void setAdaptOLED(boolean adaptOLED) {
        sp.edit().putBoolean("adaptOLED",adaptOLED).apply();
    }

    public boolean isDimScreen() {
        return sp.getBoolean("dimScreen",true);
    }

    public void setDimScreen(boolean dimScreen) {
        sp.edit().putBoolean("dimScreen",dimScreen).apply();
    }

    public int getSleepCountDown() {
        return sp.getInt("sleepCountDown",45);
    }

    public void setSleepCountDown(int sleepCountDown) {
        sp.edit().putInt("sleepCountDown",sleepCountDown).apply();
    }
}